<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="merchantId">
        <el-select v-model="queryParams.merchantId" style="width: 220px" placeholder="请选择商户"
                   @keyup.enter.native="handleQuery">
          <el-option v-for="mer in merchantList" :key="mer.id"
                     :label="mer.defindName ? mer.defindName : mer.merchantName"
                     :value="mer.merchantId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">
          删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table stripe v-loading="loading" :data="priceConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" v-if="false" align="center" prop="configId"/>
      <el-table-column label="商户ID" v-if="false" align="center" prop="merchantId"/>
      <el-table-column label="商户" :show-overflow-tooltip="true" align="center" prop="merchantName" width="180"/>
      <el-table-column label="是否启用" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
                     @change="updateStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="免运" align="center" prop="fss">
        <template slot-scope="scope"> {{ scope.row.fss ? "是" : "否" }}</template>
      </el-table-column>
      <el-table-column label="返虾币" align="center" prop="ccb">
        <template slot-scope="scope"> {{ scope.row.ccb ? "是" : "否" }}</template>
      </el-table-column>
      <el-table-column label="折扣" align="center" prop="discount"/>
      <el-table-column label="马来" align="center" prop="my"/>
      <el-table-column label="菲律宾" align="center" prop="ph"/>
      <el-table-column label="台湾" align="center" prop="tw"/>
      <el-table-column label="巴西" align="center" prop="br"/>
      <el-table-column label="泰国" align="center" prop="th"/>
      <el-table-column label="新加坡" align="center" prop="sg"/>
      <el-table-column label="哥伦比亚" align="center" prop="co"/>
      <el-table-column label="智利" align="center" prop="cl"/>
      <el-table-column label="墨西哥" align="center" prop="mx"/>
      <el-table-column label="越南" align="center" prop="vn"/>
      <el-table-column label="印尼" align="center" prop="id"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>    <!-- 添加或修改各站点价格配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商户" prop="merchantId">
              <el-select v-model="form.merchantId" placeholder="请选择商户" @change="onMerchantChange">
                <el-option v-for="mer in merchantList" :key="mer.merchantId" :label="mer.merchantName"
                           :value="mer.merchantId"></el-option>
              </el-select>
            </el-form-item>
            <el-input v-show="false" v-model="form.merchantName" placeholder="请输入商户"/>
          </el-col>
          <!-- <el-col :span="12" v-show="false">
            <el-form-item label="商户" prop="merchantName">
              <el-input v-model="form.merchantName" placeholder="请输入商户" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="免运" prop="fss">
              <el-switch v-model="form.fss" active-color="#13ce66" inactive-color="#ff4949"
                         @change="updateIsPublish(scope.row)"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="返虾币" prop="ccb">
              <el-switch v-model="form.ccb" active-color="#13ce66" inactive-color="#ff4949"
                         @change="updateIsPublish(scope.row)"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="折扣" prop="discount">
              <el-input v-model="form.discount" placeholder="请输入折扣"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="马来" prop="my">
              <el-input v-model="form.my" placeholder="请输入马来"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="台湾" prop="tw">
              <el-input v-model="form.tw" placeholder="请输入台湾"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="巴西" prop="br">
              <el-input v-model="form.br" placeholder="请输入巴西"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菲律宾" prop="ph">
              <el-input v-model="form.ph" placeholder="请输入菲律宾"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="泰国" prop="th">
              <el-input v-model="form.th" placeholder="请输入泰国"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新加坡" prop="sg">
              <el-input v-model="form.sg" placeholder="请输入新加坡"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="印尼" prop="id">
              <el-input v-model="form.id" placeholder="请输入印尼"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="越南" prop="vn">
              <el-input v-model="form.vn" placeholder="请输入越南"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="墨西哥" prop="mx">
              <el-input v-model="form.mx" placeholder="请输入墨西哥"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="哥伦比亚" prop="co">
              <el-input v-model="form.co" placeholder="请输入哥伦比亚"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="智利" prop="cl">
              <el-input v-model="form.cl" placeholder="请输入智利"/>
            </el-form-item>
            <!--            <el-form-item label="波兰" prop="pl">
                          <el-input v-model="form.pl" placeholder="请输入波兰"/>
                        </el-form-item>-->
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="西班牙" prop="es">-->
          <!--              <el-input v-model="form.es" placeholder="请输入西班牙"/>-->
          <!--            </el-form-item>-->
          <!--            <el-form-item label="法国" prop="fr">-->
          <!--              <el-input v-model="form.fr" placeholder="请输入法国"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listPriceConfig,
  getPriceConfig,
  delPriceConfig,
  addPriceConfig,
  updatePriceConfig,
} from "@/api/store/priceConfig";
import {listMerchant} from "@/api/store/shop";
import Cookies from "js-cookie";
import {listAllMerchant} from "@/api/shop/merchant";

export default {
  name: "PriceConfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //全球商户列表
      merchantList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 各站点价格配置表格数据
      priceConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantId: null,
        fss: null,
        ccb: null,
        discount: null,
        my: null,
        tw: null,
        vn: null,
        ph: null,
        th: null,
        sg: null,
        id: null,
        br: null,
        mx: null,
        co: null,
        cl: null,
        // pl: null,
        // es: null,
        // fr: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantId: [{required: true, message: "不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getMerchantList();
  },
  methods: {
    /** 查询各站点价格配置列表 */
    getList() {
      this.loading = true;
      listPriceConfig(this.queryParams).then((response) => {
        this.priceConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMerchantList() {
      listAllMerchant().then((response) => {
        this.merchantList = response.data;
        //默认选中
        if (Cookies.get('merchantId')) {
          this.queryParams.merchantId = +Cookies.get('merchantId');
        }
        this.getList();
      });
    },

    onMerchantChange(value) {
      let obj = {};
      obj = this.merchantList.find((item) => {
        return item.shopId === value;
      });
      this.form.merchantName = obj.shopName;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        merchantId: null,
        merchantName: null,
        fss: true,
        ccb: false,
        discount: 20,
        my: 35,
        tw: 35,
        vn: 35,
        ph: 35,
        th: 35,
        sg: 35,
        id: 35,
        br: 35,
        mx: 35,
        co: 35,
        cl: 35,
        // pl: 40,
        // es: 35,
        // fr: 35,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.queryParams.merchantId) {
        Cookies.set('merchantId', this.queryParams.merchantId);
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.configId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加各站点价格配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids;
      getPriceConfig(configId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改各站点价格配置";
      });
    },
    updateStatus(row) {
      let {configId, status} = row;
      updatePriceConfig({configId, status}).then((response) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.configId != null) {
            updatePriceConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPriceConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除各站点价格配置编号为"' + configIds + '"的数据项？'
        )
        .then(function () {
          return delPriceConfig(configIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
  },
};
</script>
